package com.bizmerce.rnd.oms.decision.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bizmerce.rnd.oms.decision.data.DeMinimis;
import com.bizmerce.rnd.oms.decision.data.FreeTradeAgreement;
import com.bizmerce.rnd.oms.decision.data.HsCode;
import com.bizmerce.rnd.oms.decision.data.PsrCriterion;

import egovframework.rte.psl.dataaccess.EgovAbstractDAO;

/**
 * 판정과 관련된 모든 데이터들
 * 
 * @author Gyeseon
 * 
 */
@SuppressWarnings("unchecked")
@Repository("decisionDAO")
public class DecisionDAO extends EgovAbstractDAO {

	/**
	 * FTA 협정정보를 조회합니다.
	 */
	public List<FreeTradeAgreement> selectFtas() {
		return getSqlMapClientTemplate().queryForList("Decision.selectFtas");
	}

	/**
	 * 적용여부가 'Y'인 FTA정보를 가져온다.
	 * 
	 * @return
	 * @
	 */
	public List<FreeTradeAgreement> selectApplyingFtas() {
		Map paramMap = new HashMap();
		paramMap.put("APPLYN", "Y");
		return getSqlMapClientTemplate().queryForList("Decision.selectFtas",
				paramMap);
	}

	/**
	 * 해당되는 FTA정보를 가져온다.
	 * 
	 * @param ftaCd
	 * @return
	 * @
	 */
	public FreeTradeAgreement selectFtaById(String ftaId) {
		Map paramMap = new HashMap();
		paramMap.put("AGREE_CD", ftaId);
		return (FreeTradeAgreement) getSqlMapClientTemplate().queryForObject(
				"Decision.selectFtas", paramMap);
	}

	/**
	 * 미소기준 허용 비율을 찾아온다. 농산물과, 섬유에 포함되어있는지 확인 TODO 제품의 HS코드가 적용여부가 Y이면서,
	 * 일반품목인지,농수산물인지,섬유류인지를 확인한다.
	 */
	public DeMinimis selectDeMinimisRateByFtaAndHsCode(String ftaCd,
			HsCode hsCode) {
		Map paramMap = new HashMap();
		paramMap.put("AGREE_CD", ftaCd);
		paramMap.put("HSCD", hsCode.getCode());

		DeMinimis deMinimis = (DeMinimis) getSqlMapClientTemplate()
				.queryForObject("Decision.selectDeminimisRate_special",
						paramMap);
		if (deMinimis == null) {
			deMinimis = (DeMinimis) getSqlMapClientTemplate().queryForObject(
					"Decision.selectDeminimisRate_normal", paramMap);
		}
		return deMinimis;
	}

	/**
	 * 품목별(PSR) 기준을 가져온다.
	 * 
	 * @param ftaCd
	 * @param hsCode
	 * @return
	 * @
	 */
	public List<PsrCriterion> selectPsrCriterionByFtaAndHsCode(String ftaCd,
			HsCode hsCode) {
		Map paramMap = new HashMap();
		paramMap.put("AGREE_CD", ftaCd);
		paramMap.put("HSCD", hsCode.getCode());

		List psrCriterions = getSqlMapClientTemplate().queryForList(
				"Decision.selectPsrCriterion");
		if (psrCriterions.size() == 0) {
			psrCriterions = getSqlMapClientTemplate().queryForList(
					"Decision.selectPsrCriterion_normal");
		}
		return psrCriterions;
	}
}
